﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sun : MonoBehaviour
{
    private float downPosy;
    public bool issky;
    public void Init(float downposy,float creatx,float creaty)
    {
        downPosy = downposy;
        transform.position = new Vector2(creatx, creaty);
    }
    private void Update()
    {
        if (!issky) return;
        if (transform.position.y > downPosy)
        { transform.Translate(Vector3.down * Time.deltaTime); }
        else
            Invoke("Destoryself", 10);
    }
    void Destoryself()
    {
        Destroy(gameObject);
    }

    private void OnMouseDown()
    {
        Vector3 sunNumpos = Camera.main.ScreenToWorldPoint(UImanager.instance.TextPos());
        sunNumpos = new Vector3(sunNumpos.x, sunNumpos.y, 0);
        Fly(sunNumpos);
    }
    /// <summary>
    /// 飞行后消失
    /// </summary>
    private void Fly(Vector3 pos)
    {
        StartCoroutine(Dofly(pos));
    }
    private IEnumerator Dofly( Vector3 pos)
    {
        Vector3 dir =(pos- transform.position).normalized;
        while (Vector3.Distance(pos, transform.position) > 1f)
        {
            transform.Translate(dir);
            yield return new WaitForSeconds(0.01f);
        }
        Destoryself();
        PlayerManager.instance.SunCount += 25; 
    }
    public void JumpAni()
    {
        StartCoroutine(Dojump());
    }
    private IEnumerator Dojump()
    {
        bool isRight = Random.Range(0, 2)==0;
        Vector3 startPos = transform.position;
        float posx;
        if (isRight) posx = 0.05f;
        else posx = -0.05f;
        while (transform.position.y < startPos.y + 1)
        {
            yield return new WaitForSeconds(0.01f); 
            transform.Translate(new Vector3(posx, 0.1f));
        }
        while (transform.position.y> startPos.y  )
        {
            yield return new WaitForSeconds(0.01f); 
            transform.Translate(new Vector3(posx, -0.1f));
        }
 
    }
}
